home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1035 / 1035.xpi / chrome / 1clickweather.jar / content / 1clickweather / js / data / fcst.js < prev    next >
Text File  |  2010-01-05  |  14KB  |  365 lines

  1. // ⌐ 2005 The Weather Channel Interactive, Inc.  All Rights Reserved.
  2.  
  3. oDataSources.Fcst = function(){};
  4. oDataSources.Fcst.prototype = new oDataSourcesStub;
  5.  
  6. function Forecast() {}
  7. Forecast.inherits(oDataSourcesStub);
  8. var Fcst = new Forecast();
  9.  
  10.  
  11. Forecast.prototype.setTooltip = function(panelID, dayIndex, daynight, showIcon, dayDisplayStr){
  12.    try{
  13.       var sIcon = false;
  14.       var sLabel = false;
  15.       switch(showIcon){
  16.          case "icon":
  17.             sIcon = true;
  18.             break;
  19.          case "iconandlabel":
  20.             sIcon = true;
  21.             sLabel = true;
  22.             break;
  23.          case "label":
  24.             sLabel = true;
  25.             break;
  26.       }
  27.  
  28.       var iconIndex = '';
  29.       if(this.Data['days'][dayIndex][daynight + '.t'] != 'N/A'){
  30.          iconIndex = this.Data['days'][dayIndex][daynight + '.icon'];
  31.       }else{
  32.          iconIndex = 'na';
  33.       }
  34.    }catch(e){ }
  35.  
  36.    try{
  37.       // first, we make a tooltip object
  38.       var _tooltip = document.createElement("tooltip");
  39.       _tooltip.setAttribute("id", ToolbarContainers[panelID].getID() + ".Tooltip"); // and give it an id
  40.  
  41.       // now we make an hbox
  42.       var tthbox = document.createElement("hbox");
  43.       _tooltip.appendChild(tthbox);
  44.  
  45.       // now we make a pair of vboxs
  46.       var ttvbox1 = document.createElement("vbox");
  47.       tthbox.appendChild(ttvbox1);
  48.       var ttvbox2 = document.createElement("vbox");
  49.       tthbox.appendChild(ttvbox2);
  50.  
  51.       if(sIcon){
  52.          XULSimple.Image(ttvbox1, "icons/wx/64x64/" + iconIndex + ".png", 64, 64);
  53.       }
  54.  
  55.       if(sLabel){
  56.          var l = new oXUL.Label(ttvbox2, "Forecast Summary:");
  57.          l.Bold();
  58.          l.Make();
  59.  
  60.          XULSimple.Label(ttvbox2, dayDisplayStr + " in " + this.Data['dnam']);
  61.          XULSimple.Label(ttvbox2, this.Data['days'][dayIndex][daynight + '.t']);
  62.          if(daynight == "d"){
  63.              XULSimple.Label(ttvbox2, "High of " + this.Data['days'][dayIndex]['hi'] + Degrees + this.Data['ut']);
  64.             //XULSimple.Label(ttvbox2, "High of " + this.Data['days'][dayIndex]['hi'] + " " + Degrees + this.Data['ut']);
  65.          }else{
  66.              XULSimple.Label(ttvbox2, "Low of " + this.Data['days'][dayIndex]['low'] + Degrees + this.Data['ut']);
  67.             // XULSimple.Label(ttvbox2, "Low of " + this.Data['days'][dayIndex]['low'] + " " + Degrees + this.Data['ut']);
  68.          }
  69.          if(this.Data['days'][dayIndex][daynight + '.wind.t'] == "CALM"){
  70.             XULSimple.Label(ttvbox2, "Winds: CALM");
  71.          }else{
  72.             XULSimple.Label(ttvbox2, "Winds: " + this.Data['days'][dayIndex][daynight + '.wind.t'] + " at " + this.Data['days'][dayIndex][daynight + '.wind.s'] + " " + this.Data['us']);
  73.          }
  74.           XULSimple.Label(ttvbox2, "Precip. Chance: " + this.Data['days'][dayIndex][daynight + '.ppcp'] + "%");
  75.       }
  76.  
  77.       _tooltip.setAttribute("noautohide", false);
  78.       _tooltip.setAttribute("position", "overlap");
  79.       _tooltip.position = "overlap";
  80.  
  81.       _tooltip.setAttribute("noautohide", true);
  82.       _tooltip.setAttribute("position", "before_start");
  83.       _tooltip.setAttribute("align", "center");
  84.    }catch(e){
  85.       debugLog("err in fcst tooltip: " + e);
  86.    }
  87.  
  88.    try{
  89.       var p = document.getElementById(GlobalToolbarLocation);
  90.       var t = document.getElementById(ToolbarContainers[panelID].getID() + ".Tooltip");
  91.       if(p && t)
  92.          p.removeChild(t);
  93.    }
  94.    catch(e){
  95.       debugLog("error removing tooltip: " + this.id + " " + e);
  96.    }
  97.    try{
  98.       document.getElementById(GlobalToolbarLocation).appendChild(_tooltip);
  99.    }catch(e){
  100.       debugLog("error appending tooltip: " + this.id + " " + e);
  101.    }
  102.  
  103. }
  104.  
  105. // parse an xml doc into our obs object
  106. Forecast.prototype.parseFunc = function(xmlDoc){
  107.    var configutil = new ConfigUtils();
  108.    try {
  109.       configutil.convertData(GlobalUserConfig, "Fcst", xmlDoc);
  110.    }catch(e) {
  111.       debugLog('err in fcst conversion: ' + e);
  112.    }
  113.  
  114.    this.oldData = this.Data;
  115.    this.Data = {}; // flush the Data hash
  116.  
  117.    this.parseHeader(xmlDoc);
  118.  
  119.    try{
  120.       // now we move on to the current conditions
  121.       var dayf = xmlDoc.getElementsByTagName("dayf")[0].childNodes;
  122.       var x = 0;
  123.  
  124.       // loop through all the children under the dayf node
  125.       var dayCounter = 0;
  126.       this.Data['days'] = new Array();
  127.       for(x = 0; x < dayf.length; x++){
  128.          if(dayf[x].nodeName == "day"){
  129.             this.Data['days'][dayCounter] = {};
  130.             this.Data['days'][dayCounter]['d'] = dayf[x].getAttribute("d");
  131.             this.Data['days'][dayCounter]['t'] = dayf[x].getAttribute("t");
  132.             this.Data['days'][dayCounter]['dt'] = dayf[x].getAttribute("dt");
  133.  
  134.             var fcstNode = dayf[x].childNodes;
  135.             var y = 0;
  136.  
  137.             for(y = 0; y < fcstNode.length; y++){
  138.                if(fcstNode[y].nodeName == "#text"){
  139.                   continue;
  140.                }
  141.  
  142.                if(fcstNode[y].nodeName == "part"){
  143.                   var fcstDayNight = fcstNode[y].getAttribute("p");
  144.                   var dayPart = fcstNode[y].childNodes;
  145.                   var f = 0;
  146.                
  147.                   for(f = 0; f < dayPart.length; f++){
  148.                      if(dayPart[f].nodeName == "#text"){
  149.                         continue;
  150.                      }
  151.  
  152.                      if((dayPart[f].childNodes.length == 1) && (typeof(dayPart[f].firstChild.nodeValue) == "string")){
  153.                         this.Data['days'][dayCounter][fcstDayNight + "." + dayPart[f].nodeName] = dayPart[f].firstChild.nodeValue;
  154.                      }else if(dayPart[f].childNodes.length > 1){
  155.                         var childName = dayPart[f].nodeName;
  156.                         var child = dayPart[f].childNodes;
  157.                         var c = 0;
  158.  
  159.                         for(c = 0; c < child.length; c++){
  160.                            // if this is not a valid node name, skip it
  161.                            if(child[c].nodeName == "#text"){
  162.                               continue;
  163.                            }
  164.  
  165.                            // otherwise, make a new entry in this.Data for it
  166.                            if(typeof(child[c].firstChild.nodeValue) == "string"){
  167.                               this.Data['days'][dayCounter][fcstDayNight + "." + childName + "." + child[c].nodeName] = child[c].firstChild.nodeValue;
  168.                            }
  169.                         }
  170.                      }
  171.                   }
  172.                }else if(typeof(fcstNode[y].firstChild.nodeValue) == "string"){
  173.                   this.Data['days'][dayCounter][fcstNode[y].nodeName] = fcstNode[y].firstChild.nodeValue;
  174.                }
  175.             }
  176.  
  177.             dayCounter++;
  178.          }
  179.  
  180.       }
  181.    }catch(e){
  182. //      GlobalError.Show();
  183.       debugLog("error parsing fcst data" + e);
  184.    }
  185.  
  186.    this.run++;
  187.    this.setPanel();
  188.    this.Finish();
  189. }
  190.  
  191.  
  192. Forecast.prototype.setPanel = function(){
  193.    var daynight = ""; // are we doing the high or low for the forecast
  194.    var nextDay = false; // should we bypass this day...
  195.    var displayPanelType = ""; // what kind of dipsplay type is it for the panel
  196.    var displayTooltip = ""; // is the tooltip enabled
  197.    var displayTooltipType = ""; //what kind of display type is it for the tooltip
  198.  
  199.    debugLog("creating fcst panels in Fcst.setPanel");
  200.  
  201.    // force the max days based on the extended forecast config
  202.    var maxDays = GlobalUserConfig.getAllProfiles().getDefaultProfile().getExtForecast().getExtDetails().getDays();
  203.    if(GlobalUserConfig.getAllProfiles().getDefaultProfile().getExtForecast().getIsEnabled() == "false"){
  204.       maxDays = 0;
  205.    }
  206.  
  207.    var day0Enabled = GlobalUserConfig.getAllProfiles().getDefaultProfile().getForecast().getIsEnabled();
  208.    var afterSwitch = isAfterTimeSwitch(this.Data['tm']);
  209.  
  210.    try{
  211.       for(var day in this.PanelList){
  212.          var panelID = this.PanelList[day];
  213.          var url = '';
  214.          var iconIndex = '';
  215.  
  216.          // ok, this is a bit cheesy but...
  217.          // if we are now on the first extended day and are should show tomorrow as the first forecast
  218.          // after the time switch, drop day 1 and go ahead on to day 2
  219.          if(nextDay){
  220.             day++;
  221.          }
  222.          if((panelID == "Day0") && (day0Enabled == "false")){
  223.              continue;
  224.          }
  225.  
  226.          if(day > maxDays){
  227.             continue;
  228.          }
  229.  
  230.          // ok, if this is the first day, get the normal forecast data
  231.          if(panelID == "Day0"){
  232.             displayPanelType = GlobalUserConfig.getAllProfiles().getDefaultProfile().getForecast().getType();
  233.             displayTooltip = GlobalUserConfig.getAllProfiles().getDefaultProfile().getForecast().getToolTip().getIsEnabled();
  234.             displayTooltipType = GlobalUserConfig.getAllProfiles().getDefaultProfile().getForecast().getToolTip().getType();
  235.          }else{
  236.             displayPanelType = GlobalUserConfig.getAllProfiles().getDefaultProfile().getExtForecast().getType();
  237.             displayTooltip = GlobalUserConfig.getAllProfiles().getDefaultProfile().getExtForecast().getToolTip().getIsEnabled();
  238.             displayTooltipType = GlobalUserConfig.getAllProfiles().getDefaultProfile().getExtForecast().getToolTip().getType();
  239.          }
  240.  
  241.  
  242.          if(GlobalUserConfig.getAllProfiles().getDefaultProfile().getExtForecast().getExtDetails().getTime() == "day"){
  243.             daynight = "d";
  244.          }else{
  245.             daynight = "n";
  246.          }
  247.          // now we make a pair of hboxs
  248.          var panelHbox = document.createElement("hbox");
  249.          panelHbox.setAttribute("context", ToolbarContainers[panelID].getID() + ".Tooltip");
  250. //          try{
  251.  //         panelHbox.setAttribute("id", panelID + "HBox");
  252.  //             alert(panelHbox.getAttribute("id"));
  253.  //             }catch(e) {
  254.  //             alert("111" + e);
  255.  //         }
  256.  
  257.  
  258.          var panelText = "";
  259.  
  260.          var dayDisplayStr = "";
  261.          // deal with the time forecast switheroo
  262.          var date = new Date();
  263. //         if(((date.getHours() >= 15) && (date.getHours() <= 23)) || (date.getHours() == 0)){
  264.          if(afterSwitch){
  265.             if(panelID == "Day0"){ // ok, we are between time switch and midnight and on the first fcst panel
  266.                if(GlobalUserConfig.getAllProfiles().getDefaultProfile().getForecast().getForecastSwitch() == "tonight"){
  267.                   panelText += "Tonight: " + this.Data['days'][day]['low'];
  268.                   dayDisplayStr = "Tonight";
  269.                   daynight = "n";
  270.                }else{
  271.                   day++; // jump forward to the next day
  272.                   nextDay = true;
  273.                   maxDays++; // make sure we add one more day to the max extended days
  274.                   panelText += "Tomorrow: " + this.Data['days'][day]['hi'];
  275.                   dayDisplayStr = "Tomorrow";
  276.                }
  277.             }else{
  278.                if(daynight == "d"){
  279.                   panelText += DayLookup[this.Data['days'][day]['t']] + ": " + this.Data['days'][day]['hi'];
  280.                }else{
  281.                   panelText += DayLookup[this.Data['days'][day]['t']] + ": " + this.Data['days'][day]['low'];
  282.                }
  283.                dayDisplayStr = this.Data['days'][day]['t'];
  284.             }
  285.          }else{
  286.             if(panelID == "Day0"){
  287.                panelText += "Today: " + this.Data['days'][day]['hi'];
  288.                dayDisplayStr = "Today";
  289.             }else{
  290.                if(daynight == "d"){
  291.                   panelText += DayLookup[this.Data['days'][day]['t']] + ": " + this.Data['days'][day]['hi'];
  292.                }else{
  293.                   panelText += DayLookup[this.Data['days'][day]['t']] + ": " + this.Data['days'][day]['low'];
  294.                }
  295.                dayDisplayStr = this.Data['days'][day]['t'];
  296.             }
  297.          }
  298.          // panelText += " "; // spaces between temperature value and degree character?/
  299.          panelText += Degrees; 
  300.          panelText += this.Data['ut'];
  301. //         panelText += ' ' + day + ' ' + panelID; 
  302.  
  303.  
  304.          // now we get the link to handle the clicks on the toolbar
  305.          if(day == 0){
  306.             url = this.getLink('localtoday');
  307.          }else{
  308.             url = this.getLink('localext', day);
  309.          }
  310.          panelHbox.setAttribute("uri", url);
  311.          if(this.Data['days'][day][daynight + '.t'] != 'N/A'){
  312.             iconIndex = this.Data['days'][day][daynight + '.icon'];
  313.          }else{
  314.             iconIndex = 'na';
  315.          }
  316.          switch(displayPanelType){
  317.             case 'icon':
  318.                XULSimple.Image(panelHbox, "icons/wx/20x20/" + iconIndex + ".png", 20, 20);
  319.                break;
  320.  
  321.             case 'label':
  322.                XULSimple.Label(panelHbox, panelText);
  323.                break;
  324.  
  325.             case 'iconandlabel':
  326.                XULSimple.Label(panelHbox, panelText);
  327.                XULSimple.Image(panelHbox, "icons/wx/20x20/" + iconIndex + ".png", 20, 20);
  328.                break;
  329.          }
  330.  
  331.          ToolbarContainers[panelID].Show();
  332.          ToolbarContainers[panelID].drawFromHbox(panelHbox);
  333.  
  334.          if(displayTooltip == "true"){
  335.             this.setTooltip(panelID, day, daynight, displayTooltipType, dayDisplayStr);
  336.          }
  337.  
  338.          try{
  339.           if(panelID == "Day0"){
  340.               panelHbox.addEventListener('click',interClickDay0,false);
  341.           } else if(panelID == "Day1") {
  342.               panelHbox.addEventListener('click',interClickDay1,false);
  343.           } else if(panelID == "Day2") {
  344.               panelHbox.addEventListener('click',interClickDay2,false);
  345.           } else if(panelID == "Day3") {
  346.               panelHbox.addEventListener('click',interClickDay3,false);
  347.           } else {
  348.               panelHbox.addEventListener('click',interClickDay4,false);
  349.           }
  350.               //panelHbox.setAttribute("onclick", "openLinkInNewTab('" + url + "');");
  351.           }catch(e) {
  352.               alert("ERROR: " + e);
  353.           }
  354.       }
  355.    }catch(e){
  356.       debug('error setting up fcst panels: ' + e);
  357.       debugLog("error creating fcst panels in Fcst.setPanel " + e);
  358.    }
  359. }
  360.  
  361.  
  362.  
  363.  
  364.  
  365.